/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Xceed.Silverlight.Compatibility;

namespace Xceed.Silverlight.Data
{
  internal class RawDataItem : DataItem
  {
    #region Static Fields

    private static object Lock = new object();
    private static IDictionary<Tuple<Type, string>, Func<object, object>> PropertyGetters = new Dictionary<Tuple<Type, string>, Func<object, object>>();

    #endregion Static Fields


    internal RawDataItem( object data )
      : base( data )
    {
    }


    #region IsDeleted Property

    internal bool IsDeleted
    {
      get
      {
        return m_isDeleted;
      }
    }

    private bool m_isDeleted; //false

    #endregion IsDeleted Property


    public override int GetHashCode()
    {
      return base.GetHashCode();
    }

    public override bool Equals( object obj )
    {
      RawDataItem dataItem = obj as RawDataItem;
      if( object.ReferenceEquals( dataItem, null ) )
        return false;

      return base.Equals( obj );
    }

    protected virtual bool FindDataMemberValue( string memberName, out object value )
    {
      value = null;

      object data = this.Data;

      if( data == null )
        return true;

      Func<object, object> getter = null;
      Type dataType = data.GetType();

      lock( RawDataItem.Lock )
      {
        Tuple<Type, string> key = new Tuple<Type, string>( dataType, memberName );

        if( !RawDataItem.PropertyGetters.TryGetValue( key, out getter ) )
        {
          getter = this.CreatePropertyGetter( dataType, memberName );

          if( getter != null )
          {
            // Intern the member name to improve string comparison performance
            // on future call to this method.
            memberName = string.Intern( memberName );

            // The key must be rebuild to use the interned string.
            key = new Tuple<Type, string>( dataType, memberName );

            RawDataItem.PropertyGetters.Add( key, getter );
          }
        }
      }

      if( getter == null )
        return false;

      value = getter.Invoke( data );

      return true;
    }

    internal object GetDataMemberValue( string memberName )
    {
      if( memberName == null )
        throw new ArgumentNullException( "memberName" );

      // For Phone SDK Compatibility
      if( !StringHelper.IsNullOrWhiteSpace( memberName ) )
      {
        object value;

        if( this.FindDataMemberValue( memberName, out value ) )
          return value;
      }

      throw new ArgumentException( "The specified member name does not exist.", "memberName" );
    }

    internal bool TryGetDataMemberValue( string memberName, out object value )
    {
      value = null;

      // For Phone SDK Compatibility
      if( StringHelper.IsNullOrWhiteSpace( memberName ) )
        return false;

      return this.FindDataMemberValue( memberName, out value );
    }

    internal virtual void Delete()
    {
      m_isDeleted = true;
    }

    private Func<object, object> CreatePropertyGetter( Type dataType, string memberName )
    {
      ParameterExpression parameter = Expression.Parameter( typeof( object ), null );
      Expression body = Expression.Convert( parameter, dataType );

      if( !object.Equals( memberName.Trim(), "." ) )
      {
        body = body.GetMember( memberName );
      }

      body = Expression.Convert( body, typeof( object ) );

      LambdaExpression lambda = Expression.Lambda( body, parameter );

      return ( Func<object, object> )lambda.Compile();
    }
  }
}
